import { socketGetRoomState } from "..";
import { createdRoomHandler } from "../handlers/created-room-handler";
import { gotRoomStateHandler } from "../handlers/got-room-state-handler";
import { roomLogHandler } from "../handlers/room-log-handler";
import { selectedRoomListHandler } from "../handlers/selected-room-list-handler";
import type { MessageData } from "../types/MessageData";

export const messageHandler = (message: any) => {
	try {
		const data = JSON.parse(message) as MessageData;
		switch (data.type) {
			case "roomLog":
				roomLogHandler(data.payload);
				break;
			case "selectedRoomList":
				selectedRoomListHandler(data.payload);
				break;
			case "createdRoom":
				createdRoomHandler(data.payload);
				break;
			case "gotRoomState":
				gotRoomStateHandler(data.payload);
				break;
			case "startedGame":
			case "joinedRoom":
			case "leftRoom":
			case "setedHeros":
			case "setedHiddenHeros":
			case "setedFlipped":
			case "setedSkills":
			case "setedHealthLimit":
			case "setedHealth":
			case "setedTied":
			case "shuffled":
			case "gotDiscardCards":
			case "gotDrawCards":
			case "gotPlayerCards":
			case "viewedDiscardCards":
			case "viewedDrawCards":
			case "viewedPlayerCards":
			case "transferredDrawCardsToDrawCards":
			case "transferredDrawCardsToDiscardCards":
			case "transferredDiscardCardsToDrawCards":
			case "transferredDiscardCardsToDiscardCards":
			case "transferredPlayerCardsToDiscardCards":
			case "transferredPlayerCardsToDrawCards":
			case "transferredPlayerCardsToPlayerHandCards":
			case "addedMaterielCards":
			case "addedDelayedCards":
			case "transferredDelayedCardsToPlayerDelayedCards":
			case "setDead":
				socketGetRoomState();
				break;
		}
	} catch (error) {
		console.error(error);
	}
};
